#!/usr/bin/env bash
set -e
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
# Function :CentOS7.X 安装FastDFS
# Platform :RedHatEL7.x Based Platform
# Version  :0.01
# Date     :2024-03-14
# Author   :mugoLH（今日头条：mugo老猴）
# Contact  :hxsaj@126.com
# Company  :
# depend on:
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  脚本引用 Import the script
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  变量列表 List of common variables
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----

#  函数列表 Function list
# ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
echo_p(){  printf "\e[1;36;40m%-10s\e[0m \e[1;32;40m%-1s %-30s%-1s\e[0m" "$(date '+%y%m%d_%H:%M:%S')" "Step:" "${1}" ":" >&1 ;}
info_p(){  printf "\e[1;32;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
tips_p(){  printf "\e[1;36;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}
error_p(){ printf "\e[1;35;40m%-12s %-1s\n\e[0m" "${1}" "${2}"  >&1 ;}


#　检查wget命令
function wget_chack(){
    if [[ $(whereis wget | wc -l) -ne 1 ]];then
        echo -e "[ 安装wget ]: " && yum -y -q install wget
    fi
}

#　软件安装【参数 >= 1 】
function yum_install() {
    # 步骤
    echo_p "Install softwares(yum)"
    # 执行
    local install_failed_soft
    local install_success_soft
    #  确定 yum 可用
    if [[ $(yum list available | wc -l) -eq 0 ]];then
        error_p "[ ERROR   ]" "The YUM software repository is abnormal"
        return 1
    fi

    # 从参数中取所有的参数安装
    for pkg in "$@";do
        if ! yum install -y ${pkg} >/dev/null 2>&1 ;then
            install_failed_soft[${#install_failed_soft[@]}]="${pkg}"
        else
            install_success_soft[${#install_success_soft[@]}]="${pkg}"
        fi
    done

    # 打印安装失败的软件，如果全部成功，则打印成功安装的软件
    if [ ${#install_failed_soft[@]}  -gt 0 ];then
        error_p "[ FAILED  ]" "yum install softwares(${#install_failed_soft[@]}) failed: ${install_failed_soft[*]}"
        return 1
    else
        info_p  "[ SUCCESS ]" "yum install softwares(${#install_success_soft[@]}) successfully"
    fi
}


#　安装编译环境
yum install git gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl-devel wget vim -y
#  下载
#　安装libfastcommon
git clone https://gitee.com/fastdfs100/libfastcommon.git --depth 1
cd libfastcommon/
./make.sh && ./make.sh install


#　安装libserverframe
git clone https://github.com/happyfish100/libserverframe.git --depth 1
cd libserverframe/
./make.sh && ./make.sh install


# 安装FastDFS
git clone https://gitee.com/fastdfs100/fastdfs.git --depth 1
cd fastdfs/
./make.sh && ./make.sh install

# 安装fastdfs-nginx-module
git clone https://gitee.com/fastdfs100/fastdfs-nginx-module.git --depth 1
cp fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs

